package wifiunion.com.notice.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;


import java.util.ArrayList;
import java.util.List;
import wifiunion.com.notice.GlideCircleTransform;
import wifiunion.com.notice.Constants;
import wifiunion.com.notice.R;
import wifiunion.com.notice.entity.NoticeInfoList;
import wifiunion.com.notice.entity.User;
import wifiunion.com.notice.utils.SharedPreferencesUtils;

/**
 * 通知列表适配器
 */

public class RelevanceNoticeAdapter extends RecyclerView.Adapter<RelevanceNoticeListViewHolder> {

    private Context mContext;

    private List<NoticeInfoList> data = new ArrayList<NoticeInfoList>();


    private View.OnClickListener mItemClick;

    /**
     * 0 listview
     * 1 gridview
     */
    private int mType;

    private User mUser;


    public RelevanceNoticeAdapter(Context context, int type, List<NoticeInfoList> dt, View.OnClickListener itemClick) {
        mContext = context;
        mType = type;
        data = dt;
        mItemClick = itemClick;
        mUser = SharedPreferencesUtils.getUserSharedPreferences(mContext);
    }


    @Override
    public RelevanceNoticeListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view;

         view = LayoutInflater.from(parent.getContext()).inflate(R.layout.relevancenotice_grid_item, parent, false);


        RelevanceNoticeListViewHolder cv = new RelevanceNoticeListViewHolder(view, mType);


        return cv;
    }

    @Override
    public void onBindViewHolder(final RelevanceNoticeListViewHolder holder, int position) {


        final NoticeInfoList noticeInfo = data.get(position);

            //网格展示
            if (null != noticeInfo.getFromUser() && noticeInfo.getFromUser().size() > 0) {
                if (noticeInfo.getFromUser().size() == 1) {

                    holder.singleHeadLL.setVisibility(View.VISIBLE);
                    holder.doubleHeadLL.setVisibility(View.GONE);
                    holder.threeHeadLL.setVisibility(View.GONE);

                    if (!"0".equals(noticeInfo.getFromUser().get(0).getUuid())) {
                        Glide.with(mContext)
                                .load(Constants.HTTP_URL_IMAGESERVER + noticeInfo.getFromUser().get(0).getComparisonPic())
                                .error(R.drawable.default_header).centerCrop()
                                .transform(new GlideCircleTransform(mContext)).into(holder.mNoticegridSingleheaderiv);
                    } else {
                        // 陌生人
                        Glide.with(mContext)
                                .load(R.drawable.msr)
                                .error(R.drawable.msr).fitCenter()
                                .transform(new GlideCircleTransform(mContext)).into(holder.mNoticegridSingleheaderiv);

//                        holder.mNoticegridSingleheaderiv.setBackgroundResource(R.drawable.msr);

                    }


                    holder.mNoticegridsbdxtv.setText(noticeInfo.getFromUser().get(0).getName() == null ? "未登记人员" : noticeInfo.getFromUser().get(0).getName());

                } else if (noticeInfo.getFromUser().size() == 2) {

                    holder.singleHeadLL.setVisibility(View.GONE);
                    holder.doubleHeadLL.setVisibility(View.VISIBLE);
                    holder.threeHeadLL.setVisibility(View.GONE);

                    if (!"0".equals(noticeInfo.getFromUser().get(0).getUuid())) {
                        Glide.with(mContext)
                                .load(Constants.HTTP_URL_IMAGESERVER + noticeInfo.getFromUser().get(0).getComparisonPic())
                                .error(R.drawable.default_header).centerCrop()
                                .transform(new GlideCircleTransform(mContext)).into(holder.mNoticegriDoubleheader1iv);
                    } else {
                        Glide.with(mContext)
                                .load(R.drawable.msr)
                                .error(R.drawable.msr).fitCenter()
                                .transform(new GlideCircleTransform(mContext)).into(holder.mNoticegriDoubleheader1iv);
                    }
                    if (!"0".equals(noticeInfo.getFromUser().get(1).getUuid())) {
                        Glide.with(mContext)
                                .load(Constants.HTTP_URL_IMAGESERVER + noticeInfo.getFromUser().get(1).getComparisonPic())
                                .error(R.drawable.default_header).centerCrop()
                                .transform(new GlideCircleTransform(mContext)).into(holder.mNoticegriDoubleheader2iv);
                    } else {
                        Glide.with(mContext)
                                .load(R.drawable.msr)
                                .error(R.drawable.msr).fitCenter()
                                .transform(new GlideCircleTransform(mContext)).into(holder.mNoticegriDoubleheader2iv);
                    }
                    holder.mNoticegridsbdxtv.setText((noticeInfo.getFromUser().get(0).getName() == null ? "未登记人员" : noticeInfo.getFromUser().get(0).getName()) + ";" + (noticeInfo.getFromUser().get(1).getName() == null ? "未登记人员" : noticeInfo.getFromUser().get(1).getName()));

                } else if (noticeInfo.getFromUser().size() > 2) {

                    holder.singleHeadLL.setVisibility(View.GONE);
                    holder.doubleHeadLL.setVisibility(View.GONE);
                    holder.threeHeadLL.setVisibility(View.VISIBLE);

                    if (!"0".equals(noticeInfo.getFromUser().get(0).getUuid())) {
                        Glide.with(mContext)
                                .load(Constants.HTTP_URL_IMAGESERVER + noticeInfo.getFromUser().get(0).getComparisonPic())
                                .error(R.drawable.default_header).centerCrop()
                                .transform(new GlideCircleTransform(mContext)).into(holder.mNoticegriThreeheader1iv);
                    } else {
                        Glide.with(mContext)
                                .load(R.drawable.msr)
                                .error(R.drawable.msr).fitCenter()
                                .transform(new GlideCircleTransform(mContext)).into(holder.mNoticegriThreeheader1iv);

                    }

                    if (!"0".equals(noticeInfo.getFromUser().get(1).getUuid())) {
                        Glide.with(mContext)
                                .load(Constants.HTTP_URL_IMAGESERVER + noticeInfo.getFromUser().get(1).getComparisonPic())
                                .error(R.drawable.default_header).centerCrop()
                                .transform(new GlideCircleTransform(mContext)).into(holder.mNoticegriThreeheader2iv);
                    } else {
                        Glide.with(mContext)
                                .load(R.drawable.msr)
                                .error(R.drawable.msr).fitCenter()
                                .transform(new GlideCircleTransform(mContext)).into(holder.mNoticegriThreeheader2iv);
                    }

                    if (!"0".equals(noticeInfo.getFromUser().get(2).getUuid())) {
                        Glide.with(mContext)
                                .load(Constants.HTTP_URL_IMAGESERVER + noticeInfo.getFromUser().get(2).getComparisonPic())
                                .error(R.drawable.default_header).centerCrop()
                                .transform(new GlideCircleTransform(mContext)).into(holder.mNoticegriThreeheader3iv);

                    } else {
                        Glide.with(mContext)
                                .load(R.drawable.msr)
                                .error(R.drawable.msr).fitCenter()
                                .transform(new GlideCircleTransform(mContext)).into(holder.mNoticegriThreeheader3iv);
                    }


                    holder.mNoticegridsbdxtv.setText((noticeInfo.getFromUser().get(0).getName() == null ? "未登记人员" : noticeInfo.getFromUser().get(0).getName()) + ";" + (noticeInfo.getFromUser().get(1).getName() == null ? "未登记人员" : noticeInfo.getFromUser().get(1).getName()) + ";" + (noticeInfo.getFromUser().get(2).getName() == null ? "未登记人员" : noticeInfo.getFromUser().get(2).getName()));


                }

            } else {
                holder.singleHeadLL.setVisibility(View.GONE);
                holder.doubleHeadLL.setVisibility(View.GONE);
                holder.threeHeadLL.setVisibility(View.GONE);
                holder.mNoticegridsbdxtv.setText("");
//                holder.mNoticegridsbdxtv.setText("");

            }

//            if (null != noticeInfo.getStartTime() && null != noticeInfo.getEndTime()) {
//                holder.mNoticegridtimestv.setText(noticeInfo.getStartTime().substring(0, noticeInfo.getStartTime().length() - 3) + "~" + noticeInfo.getEndTime().substring(0, noticeInfo.getEndTime().length() - 3));
//            } else {
//                holder.mNoticegridtimestv.setText("");
//            }

//            if(noticeInfo.getIsdate() == 1)
//            {
//                holder.mNoticegridtimestv.setText(noticeInfo.getStartDate() +" " + noticeInfo.getStartTime().substring(0, noticeInfo.getStartTime().length() - 3) + " ~ "+"\n" +noticeInfo.getEndDate() +" " +noticeInfo.getEndTime().substring(0, noticeInfo.getEndTime().length() - 3));
//            }
//            else
//            {
//                holder.mNoticegridtimestv.setText("不限");
//            }


            if (!TextUtils.isEmpty(noticeInfo.getCycle())) {
                StringBuffer sb = new StringBuffer();
                ArrayList<String> list = new ArrayList<>();
                if (noticeInfo.getCycle().contains("1")) {
                    list.add("一");
                }
                if (noticeInfo.getCycle().contains("2")) {
                    list.add("二");
                }
                if (noticeInfo.getCycle().contains("3")) {
                    list.add("三");
                }
                if (noticeInfo.getCycle().contains("4")) {
                    list.add("四");
                }
                if (noticeInfo.getCycle().contains("5")) {
                    list.add("五");
                }
                if (noticeInfo.getCycle().contains("6")) {
                    list.add("六");
                }
                if (noticeInfo.getCycle().contains("7")) {
                    list.add("日");
                }

                if (null != list && list.size() > 0) {
                    for (int i = 0; i < list.size(); i++) {
                        sb.append(list.get(i));
                        if (i < list.size() - 1) {
                            sb.append(",");
                        }
                    }

                }
                holder.mNoticegridweektv.setText(sb.toString());

            } else {
                holder.mNoticegridweektv.setText("");
            }


            if (noticeInfo.getIsdate() == 1) {
                holder.mNoticegriddatetv.setText(noticeInfo.getStartDate() + " ~ " + noticeInfo.getEndDate());
            } else {
                holder.mNoticegriddatetv.setText("不限");
            }


            if (!TextUtils.isEmpty(noticeInfo.getStartTime()) && !TextUtils.isEmpty(noticeInfo.getEndTime())) {
                holder.mNoticegridtimetv.setText(noticeInfo.getStartTime().substring(0, noticeInfo.getStartTime().length() - 3) + " ~ " + noticeInfo.getEndTime().substring(0, noticeInfo.getStartTime().length() - 3));
            } else {
                holder.mNoticegridtimetv.setText("");
            }


//            if (null != noticeInfo.getFromDevice() && noticeInfo.getFromDevice().size() > 0) {
//                holder.mNoticegridsbsbtv.setText(noticeInfo.getFromDevice().get(0).getLocationName());
//            } else {
//                holder.mNoticegridsbsbtv.setText("");
//            }


            if (null != noticeInfo.getFromDevice() && noticeInfo.getFromDevice().size() > 0) {
                switch (noticeInfo.getFromDevice().size()) {
                    case 1:
                        holder.mNoticegridsbsbtv.setText(noticeInfo.getFromDevice().get(0).getLocationName());
                        break;
                    case 2:
                        holder.mNoticegridsbsbtv.setText(noticeInfo.getFromDevice().get(0).getLocationName() + " " + noticeInfo.getFromDevice().get(1).getLocationName());
                        break;
                    case 3:
                        holder.mNoticegridsbsbtv.setText(noticeInfo.getFromDevice().get(0).getLocationName() + " " + noticeInfo.getFromDevice().get(1).getLocationName() + " " + noticeInfo.getFromDevice().get(2).getLocationName());
                        break;
                }

            } else {
                holder.mNoticegridsbsbtv.setText("");
            }


            //通知内容
            holder.mNoticegridtznrtv.setText(noticeInfo.getContent());
            //通知分组

//            if (!TextUtils.isEmpty(noticeInfo.getGroupName())) {
//                holder.mNoticegridGrouptv.setText(noticeInfo.getGroupName());
//            } else {
//                holder.mNoticegridGrouptv.setText("不限");
//            }

            if (noticeInfo.getState() == 2) {
                holder.mNoticeOverdueIv.setVisibility(View.VISIBLE);
            } else {
                holder.mNoticeOverdueIv.setVisibility(View.GONE);
            }

            holder.mNoticegriddeletetv.setTag(position);
            holder.mMerbergridedittv.setTag(position);
            holder.mNoticegridlooktv.setTag(position);
            holder.mNoticegridRecordtv.setTag(position);
            holder.mNoticegridcopytv.setTag(position);

            holder.mNoticegridlooktv.setOnClickListener(mItemClick);
            holder.mNoticegriddeletetv.setOnClickListener(mItemClick);
            holder.mMerbergridedittv.setOnClickListener(mItemClick);
            holder.mNoticegridRecordtv.setOnClickListener(mItemClick);
            holder.mNoticegridcopytv.setOnClickListener(mItemClick);


            if (Constants.ISONLINE_VERSION) {
                holder.mNoticeGridPathrl.setVisibility(View.VISIBLE);
                holder.mNoticeGridPathrl.setTag(position);
                holder.mNoticeGridPathrl.setOnClickListener(mItemClick);
            } else {
                holder.mNoticeGridPathrl.setVisibility(View.GONE);
            }


        if (noticeInfo.getChecked() == 1) {
            holder.mChooseStateIv.setSelected(true);
        } else {
            holder.mChooseStateIv.setSelected(false);
        }

        holder.mChooseStateIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(v.isSelected())
                {
                    noticeInfo.setChecked(0);
                    v.setSelected(false);
                }else
                {
                    noticeInfo.setChecked(1);
                    v.setSelected(true);
                }
                notifyDataSetChanged();
            }
        });


    }

    @Override
    public int getItemCount() {
        return data.size();
    }
}
